package com.whitehot.ndkmaster;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Random;


public class MainActivity extends AppCompatActivity
{
    public static final String KEY = "HHR40Y0X0P0IRH460P0IRH46";
    public static final char seeds[] =
            {
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                    'w', 'x', 'y', 'z', '0', '1', '2', '3',
                    '4', '5', '6', '7', '8', '9', '+', '/'
            };
    TextView et;

    public static String getPhoneId(Context ctx)
    {
        String m_szAndroidID = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(m_szAndroidID))
        {
            return "";
        } else
        {
            return m_szAndroidID;
        }
    }

    public static String getDeviceSN()
    {
        String var0 = null;
        try
        {
            Class var1 = Class.forName("android.os.SystemProperties");
            Method var2 = var1.getMethod("get", new Class[]{String.class, String.class});
            var0 = (String) ((String) var2.invoke(var1, new Object[]{"ro.serialno", "unknown"}));
        } catch (Exception var3)
        {
        }
        if (TextUtils.isEmpty(var0))
        {
            var0 = "";
        }
        return var0;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (TextView) findViewById(R.id.et);

        check();
    }

    private void check()
    {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
    }

    public void click(View view)
    {
//        StringBuilder sb = new StringBuilder();
//        Random rd = new Random();
//        for(int i = 0;i<100000;i++)
//        {
//            sb.append(seeds[rd.nextInt(64)]);
//        }
//        for(int i = 0;i<100000;i++)
//        {
//            sb.append(seeds[rd.nextInt(64)]);
//        }
//        long s = SystemClock.uptimeMillis();
//        String res = NativeX.aesEncrypt(KEY, sb.toString());
//        long e = SystemClock.uptimeMillis();
//        Log.e("s", "用时：" + (e-s) + "ms  res->" + res);
//        Toast.makeText(this, NativeX.aesCbcEncrypt(KEY, KEY, sb.toString()), Toast.LENGTH_SHORT).show();
//        Toast.makeText(this, AESUtils.encryptData(KEY, "123"), Toast.LENGTH_SHORT).show();;
//        NativeX.readString(Environment.getExternalStorageDirectory() +"/1.txt");
//        Log.e("s", Build.FINGERPRINT);
//        Toast.makeText(this, NativeX.getDeviceCode(this), Toast.LENGTH_SHORT).show();

//        rndFile();

//        Log.e("s", NativeX.decodeBase64("dGhpvdf2lzYXdzb"));
//        Toast.makeText(this, NativeX.encodeBase64("thisisawsome"), Toast.LENGTH_SHORT).show();
        String s = NativeX.getDeviceCode(this);
        Log.e("s", s);
    }

    private File getRootDir()
    {
        File fir = getExternalFilesDir(null);
        if (fir != null)
        {
            File f = new File(fir, "/test/test");
            if (!f.exists())
            {
                f.mkdirs();
            }
            return f;
        }
        return null;
    }

    private void rndFile()
    {
        File f = getRootDir();
        for (int i = 0; i < 10; i++)
        {
            File pl = new File(f, "/" + getName());
            if (!pl.exists())
            {
                pl.mkdir();
            }
            File txt = new File(pl, "/" + getName() + ".dat");
            if (!txt.exists())
            {
                try
                {
                    txt.createNewFile();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    private String getName()
    {
        Random rd = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++)
        {
            sb.append((char) (rd.nextInt(26) + 'a'));
        }
        return sb.toString();
    }

    public void click2(View view)
    {
        String s = et.getText().toString();
//        Log.e("s", NativeClass.aesDecrypt("zxcvbnmasdfghjkl", s));
    }
}
